<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
        <!-- 搜索框 -->
        <!-- 点击搜索和在点击由 clearable 属性生成的清空按钮时触发clear -->
       <el-input style="width:30%" clearable  @clear="getGoodsList"  v-model="queryInfo.query" placeholder="请输入内容" class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
      </el-input>
      <!-- 商品列表 编程式跳转this.$router.push('')  this可以省略-->
      <el-button type="primary" style="margin-left:20px" @click="$router.push('/goods/add')">添加商品</el-button>
      <el-table :data="goodsList" border >
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name">
        </el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="120">
        </el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="100">
        </el-table-column>
        <el-table-column label="创建时间"  width="100">
          <template slot-scope="scope" >
            {{ scope.row.add_time | dateFormat('yyyy-MM-dd') }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="delGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {

  data () {
    return {
      goodsList:[],
      queryInfo: {
        query:'', // 查询参数
        pagenum:1,
        pagesize:10
      },
      total:0
    }
  },

  methods: {
    // 获取商品列表
    async getGoodsList() {
      const {data:res} = await this.$http.get('goods',{
        params:this.queryInfo
      })
      if(res.meta.status == 200) {
        this.goodsList = res.data.goods
        this.total = res.data.total
        // console.log(this.goodsList)
        // console.log(this.total)
      }
    },
    // 改变当前多少条
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    // 改变当前页数
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()

    },
    // 删除商品
    async delGoods(row) {
      // console.log(row)
      // return
      // 删除之前与用户确认
      const result = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err=>err)
      if(result !== 'confirm') return this.$message.info('取消删除')
      const {data:res} = await this.$http.delete(`goods/${row.goods_id}`)
      if(res.meta.status == 200) {
        this.$message.success('删除成功')
        this.getGoodsList()
      }
      
    }
  },

  created () {
    this.getGoodsList()
  }
}
</script>

<style lang='less' scoped>

</style>
